<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 登录表单 -->
    <van-form ref="loginForm" @submit="onSubmit">
      <van-cell-group>
        <van-field
          v-model="user.mobile"
          name="mobile"
          placeholder="请输入手机号"
          :rules="userRules.mobile"
          maxlength="11"
          type="number"
        >
          <i slot="left-icon" class="toutiao toutiao-shouji"></i>
        </van-field>

        <van-field
          v-model="user.code"
          name="code"
          placeholder="请输入验证码"
          :rules="userRules.code"
          maxlength="6"
          type="number"
        >
          <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
          <template #button>
            <!-- 倒计时组件 -->
            <van-count-down
              @finish="isCountDown = false"
              v-if="isCountDown"
              :time="1000 * 60"
              format="ss s"
            />
            <van-button
              v-else
              native-type="button"
              @click="onSendSms"
              class="send-sms-btn"
              round
              size="small"
              type="default"
              >发送验证码</van-button
            >
          </template>
        </van-field>
      </van-cell-group>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>
<script>
import { login, sendSms } from '@/api/user.js'
export default {
  name: 'LoginIndex',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '13911111111', // 手机号
        code: '246810' // 验证码
      },
      userRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
          }
        ],
        code: [
          { required: true, message: '验证码格式错误' },
          {
            pattern: /^\d{6}$/
          }
        ]
      },
      isCountDown: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    /*
      1-获取表单数据
      2-todo：表单验证
      3-提交表单请求登录
      4-根据请求响应结果处理后续操作 */
    async onSubmit () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间，默认是2000，如果为0则持续展示
      })
      try {
        const user = this.user
        const { data } = await login(user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (error) {
        if (error.response.status === 400) {
          console.log('手机号或验证码错误')
          this.$toast.fail('手机号或验证码错误')
        } else {
          console.log('登录失败，请稍后重试')
          this.$toast.fail('登录失败，请稍后重试')
        }
      }
    },
    async onSendSms () {
      /*
        1.校验手机号
        2.校验成功，发送验证码倒计时执行 */
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (error) {
        //   return和后面连写是为了阻止继续往下执行
        return console.log('验证失败', error)
      }
      this.isCountDown = true

      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (error) {
        //   发送失败，关闭倒计时窗口
        this.isCountDown = false
        if (error.response.code === 429) {
          this.$toast('温馨提示：输入频繁，请稍后重试')
        } else {
          console.log('发送失败', error.message)
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px/2;
  }
  .send-sms-btn {
    width: 100px;
    height: 46px;
    line-height: 46px/2;
    background-color: #ededed;
    font-size: 11px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px/2 33px/2;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
